package com.jz.partclass.conroller;

import com.jz.partclass.model.*;
import com.jz.partclass.service.PartService;
//import com.jz.subclass.test.SubStatusEnum;
import com.jz.util.ResultBuildVo;
import com.jz.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by WISE on 2021/12/26.
 */
@RestController
@CrossOrigin//开启跨域
public class PartController {


    //1.首页分页
    @Autowired
    private PartService partService;

    @PostMapping("spare/queryPage")
    public ResultVo firstPage(@RequestBody PartSelectVo partSelectVo) {
        ResultVo resultVo = partService.firstPage(partSelectVo);

        return resultVo;
    }

    //法人下拉框
    @GetMapping("legalPerson/ofCurrentUser")
    public ResultVo ofCurrentUser() {

        Integer userId = 1;

        return partService.queryByUserId(userId);
    }

    //备件小类下拉框
    @GetMapping("partClass/queryAll")
    public ResultVo queryAll() {

        return partService.queryAll();
    }

    //计划大类下拉框
    @GetMapping("partClass/queryAll2")
    public ResultVo queryAll2() {

        return partService.queryAll2();
    }


    //2.新增
    @PostMapping("partClass/add")
    @Transactional
    public ResultVo add(@RequestBody @Valid PartAddVo partAddVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = partService.addpartClass(partAddVO);
        return resultVo;
    }


//    //3.修改
//    @GetMapping("queryById")
//    @Transactional
//    public ResultVo queryById(Integer id) {
//
//        if (id == null) {
//            return ResultBuildVo.error("ID不能为空");
//        }
//        return partService.queryById(id);
//    }


    @PostMapping("part/update")
    public ResultVo update(@RequestBody @Valid PartUpdateVo partUpdateVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return partService.updateSubClass(partUpdateVo);
    }

    @PostMapping("partClass/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid PartUpdateStatusVo partUpdateStatusVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return partService.updateStatus(partUpdateStatusVo);
    }


}



